from pydantic import BaseModel
from typing import Optional
from datetime import datetime


# 用户相关
class UserBase(BaseModel):
    username: str


class UserCreate(UserBase):
    password: str
    role: Optional[str] = "operator"


class UserLogin(BaseModel):
    username: str
    password: str


class UserLogout(BaseModel):
    access_token: str


class User(UserBase):
    id_: int
    role: str
    is_active: bool
    created_at: datetime

    class Config:
        from_attributes = True


class Token(BaseModel):
    access_token: str
    token_type: str


# 学生相关
class StudentBase(BaseModel):
    student_id: str
    name: str
    phone: Optional[str] = None


class StudentCreate(StudentBase):
    pass


class Student(StudentBase):
    id_: int
    balance: float
    status: str
    created_at: datetime

    class Config:
        from_attributes = True

class RechargeCreate(BaseModel):
    student_id: int
    amount: float
    notes: Optional[str] = None


# 机房相关
class ComputerRoomBase(BaseModel):
    name: str
    location: Optional[str] = None
    total_computers: int


class ComputerRoomCreate(ComputerRoomBase):
    hourly_rate: float = 5.0


class ComputerRoom(ComputerRoomBase):
    id_: int
    available_computers: int
    status: str
    hourly_rate: float
    created_at: datetime

    class Config:
        from_attributes = True


# 机器相关
class MachineBase(BaseModel):
    machine_number: str
    ip_address: Optional[str] = None


class MachineCreate(MachineBase):
    room_id: int


class Machine(MachineBase):
    id_: int
    room_id: int
    status: str
    created_at: datetime

    class Config:
        from_attributes = True


# 上机会话相关
class SessionStart(BaseModel):
    student_id: int
    machine_id: int


class SessionEnd(BaseModel):
    session_id: int


class ComputerSession(BaseModel):
    id_: int
    student_id: int
    machine_id: int
    start_time: datetime
    end_time: Optional[datetime]
    duration_minutes: Optional[int]
    hourly_rate: float
    total_cost: Optional[float]
    status: str

    class Config:
        from_attributes = True

class SessionHistoryItem(BaseModel):
    # ComputerSession 字段
    id_: int
    student_id: int
    machine_id: int
    start_time: datetime
    end_time: Optional[datetime]
    duration_minutes: Optional[int]
    total_cost: Optional[float]
    hourly_rate: float
    status: str
    created_at: datetime
    
    # 关联信息
    student_number: str
    student_name: str
    room_name: str
    machine_number: str
    
    class Config:
        from_attributes = True